package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
	protected double balance;

	/**
	 * Creates an account.
	 * 
	 * @param amount
	 *            initial amount of funds, must be positive
	 */
	public AbstractAccount(final double amount) {
		if (amount < 0) {
			throw new IllegalArgumentException(
					"Initial amount of funds cannot be negative.");
		}
		this.balance = amount;
	}

	/**
	 * @inheritDoc
	 */
	public double getBalance() {
		return balance;
	}

	/**
	 * @inheritDoc
	 */
	public void deposit(final double amount) {
		if (amount > 0) {
			balance += amount;
		} else {
			throw new IllegalArgumentException(
					"Amount of funds to deposit must be positive.");
		}
	}

	/**
	 * @inheritDoc
	 */
	public void withdraw(final double amount) throws NotEnoughFundsException {
		if (amount > 0) {
			if (balance >= amount) {
				balance -= amount;
			} else {
				throw new NotEnoughFundsException(amount - balance);
			}
		} else {
			throw new IllegalArgumentException(
					"Amount of funds to withdraw must be positive.");
		}
	}

	/**
	 * @inheritDoc
	 */
	public double maximumAmountToWithdraw() {
		return balance;
	}
}
